<?php

	/**
	 * Class that represents the validation routines helper
	 *
	 * @version 1.0
	 * @package Helpers
	 */
	class Validator extends GlobalController
	{
		/**
		 * Parses given value according to the given rules and returns an error if
		 * validation failed
		 *
		 * @param $value validation value to parse
		 * @param $rules array of needed validation rules (required, email, integer, float, url, ip, min_value, max_value, min_length, max_length)
		 * @param $css_class css class of returned value if validation failed
		 */
		public static function ValidateField($value, $rules)
		{
			$validation_results = null;

			//Parse each rule
			foreach ($rules as $rule_name => $rule)
			{
				switch ($rule_name)
				{
					case 'required':
						(!self::IsNonEmpty($value)) ? $validation_results['required'] = $rule['text'] : '';

						break;

					case 'email':
						(!self::IsEmail($value)) ? $validation_results['email'] = $rule['text'] : '';

						break;

					case 'integer':
						(!self::IsInteger($value)) ? $validation_results['integer'] = $rule['text'] : '';

						break;

					case 'float':
						(!self::IsFloat($value)) ? $validation_results['float'] = $rule['text'] : '';

						break;

					case 'url':
						(!self::IsUrl($value)) ? $validation_results['url'] = $rule['text'] : '';

						break;

					case 'ip':
						(!self::IsIp($value)) ? $validation_results['ip'] = $rule['text'] : '';

						break;

					case 'min_value':
						(!self::CheckMinValue($value, $rule['value'])) ? $validation_results['min_value'] = $rule['text'] : '';

						break;

					case 'max_value':
						(!self::CheckMaxValue($value, $rule['value'])) ? $validation_results['max_value'] = $rule['text'] : '';

						break;

					case 'min_length':
						(!self::CheckMinLength($value, $rule['value'])) ? $validation_results['min_length'] = $rule['text'] : '';

						break;

					case 'max_length':
						(!self::CheckMaxLength($value, $rule['value'])) ? $validation_results['max_length'] = $rule['text'] : '';

						break;

					case 'regexp':
						(!self::CheckRegExp($value, $rule['value'])) ? $validation_results['regexp'] = $rule['text'] : '';

						break;
					
					//TODO: to make it work
//					case 'date':
//						(!self::IsDate($value, $rule['value'])) ? $validation_results['date'] = $rule['text'] : '';
//						
//						break;

				}
			}
			return $validation_results;
		}

		/**
		 * Validates POST array according to the given rules
		 *
		 * @param $rules array of needed validation rules. E.g.: array('login' => array('required' => 'Can not be empty'), 'email' => array('required' => 'Can not be empty', 'email' => 'Is not valid email')));
		 * @return array|false validation results
		 */
		public static function Validate($rules)
		{
			if (!empty(HTTPContext::${strtolower(HTTPContext::$request_method)}))
			{
				//loops through data array
				foreach (HTTPContext::${strtolower(HTTPContext::$request_method)} as $name => $value)
				{
					if (!isset($rules[$name])) continue;

					//validates the field
					$result[$name] = self::ValidateField($value, $rules[$name]);

					//return NULL if no errors found
					if ($result[$name] == null) unset($result[$name]);
				}
				return $result;
			}
			else
			{
				return false;
			}
		}

		/**
		 * Check if given data is empty or null
		 *
		 * @param $value validation value to parse
		 *
		 * @return boolean true if empty or null, otherwise false
		 */
		private static function IsNonEmpty($value)
		{
			return !(($value == null) || ($value === ""));
		}

		/**
		 * Check if given data is valid email
		 *
		 * @param $value validation value to parse
		 *
		 * @return boolean true if valid email, otherwise false
		 */
		private static function IsEmail($value)
		{
			return filter_var($value, FILTER_VALIDATE_EMAIL);
		}

		/**
		 * Check if given data is integer
		 *
		 * @param $value validation value to parse
		 *
		 * @return boolean true if valid integer, otherwise false
		 */
		private static function IsInteger($value)
		{
			return filter_var($value, FILTER_VALIDATE_INT);
		}

		/**
		 * Check if given data is float
		 *
		 * @param $value validation value to parse
		 *
		 * @return boolean true if valid float, otherwise false
		 */
		private static function IsFloat($value)
		{
			return filter_var($value, FILTER_VALIDATE_FLOAT);
		}

		/**
		 * Check if given data is URL
		 *
		 * @param $value validation value to parse
		 *
		 * @return boolean true if valid URL, otherwise false
		 */
		private static function IsUrl($value)
		{
			return filter_var($value, FILTER_VALIDATE_URL);
		}

		/**
		 * Check if given data is IP address
		 *
		 * @param $value validation value to parse
		 *
		 * @return boolean true if valid IP address (v4 and v6 are allowed), otherwise false
		 */
		private static function IsIp($value)
		{
			return filter_var($value, FILTER_VALIDATE_IP);
		}

		/**
		 * Checks whether the value is greater then rule
		 *
		 * @param $value value to be checked
		 * @param $rule validation rule value
		 * @return boolean
		 */
		private static function CheckMinValue($value, $rule)
		{
			return ($value >= $rule);
		}

		/**
		 * Checks whether the value is less then rule
		 *
		 * @param $value value to be checked
		 * @param $rule validation rule value
		 * @return boolean
		 */
		private static function CheckMaxValue($value, $rule)
		{
			return ($value <= $rule);
		}

		/**
		 * Checks whether the value length is greater then rule
		 *
		 * @param $value value to be checked
		 * @param $rule validation rule value
		 * @return boolean
		 */
		private static function CheckMinLength($value, $rule)
		{
			return (strlen($value) >= $rule);
		}

		/**
		 * Checks whether the value length is less then rule
		 *
		 * @param $value value to be checked
		 * @param $rule validation rule value
		 * @return boolean
		 */
		private static function CheckMaxLength($value, $rule)
		{
			return (strlen($value) <= $rule);
		}

		/**
		 * Checks whether the value corresponds the regular expression rule
		 *
		 * @param $value value to be checked
		 * @param $rule validation rule regular expression
		 * @return boolean
		 */
		private static function CheckRegExp($value, $rule)
		{
			return ereg($rule, $value);
		}

		/**
		 * TODO: Checks whether the value is a date with the specified format
		 * 
		 * @param $value value to be checked
		 * @param $rule validation rule regular expression
		 * @return boolean
		 */
		private static function IsDate($value, $rule)
		{
			//return ereg($rule, $value);
		}
	
		
	}

?>